﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Xml;

using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client.Configuration;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm;

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Crm.Services.Utility;

namespace CRM2ORCH
{
    public class Create
    {
        public OrganizationService service;
        public string message = string.Empty;

        public string createEntity(String entity, String attributeName, String attributeValue)
        {
            try
            {
                Connection conn = new Connection();
                service = conn.setConnection();
                Entity newentity = new Entity(entity);
                if (!attributeName.Contains(";"))
                {
                    //create object with single attribute
                    newentity.Attributes[attributeName] = attributeValue;
                }
                // use the service to create entity object
                else
                {
                    String[] attributeNameArray = attributeName.Split(';');
                    String[] attributeValueArray = attributeValue.Split(';');
                    for (int i = 0; i < attributeNameArray.Length; i++)
                    {
                        try
                        {
                            newentity.Attributes[attributeNameArray[i]] = attributeValueArray[i];
                        }
                        catch (Exception e)
                        {
                            newentity.Attributes[attributeNameArray[i]] = "null";
                        }
                    }
                }
                service.Create(newentity);

            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }
    }
}